//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SingleProductComposite.h"
using namespace Cephei::QL::Models::Marketmodels::Products;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProduct.h>
#include <gen/QL/Models/Marketmodels/Products/MarketModelComposite.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::CSingleProductComposite (boost::shared_ptr<QuantLib::SingleProductComposite>& childNative, Object^ owner) : CMarketModelComposite(CSingleProductComposite::typeid)
{
#ifdef HANDLE
	_phSingleProductComposite = NULL;
#endif
	_ppSingleProductComposite = &childNative;
    _ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (boost::dynamic_pointer_cast<QuantLib::MarketModelComposite> (*_ppSingleProductComposite));
}
Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::CSingleProductComposite (QuantLib::SingleProductComposite& childNative, Object^ owner) : CMarketModelComposite(CSingleProductComposite::typeid)
{
#ifdef HANDLE
	_phSingleProductComposite = NULL;
#endif
	_ppSingleProductComposite = new boost::shared_ptr<QuantLib::SingleProductComposite> (&childNative);
    _ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (boost::dynamic_pointer_cast<QuantLib::MarketModelComposite> (*_ppSingleProductComposite));
    _SingleProductCompositeOwner = owner;
    _MarketModelCompositeOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::CSingleProductComposite (CSingleProductComposite^ copy) : CMarketModelComposite(CSingleProductComposite::typeid)
{
#ifdef HANDLE
	_phSingleProductComposite = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSingleProductComposite = new boost::shared_ptr<QuantLib::SingleProductComposite> (copy->GetShared());
        _ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (boost::dynamic_pointer_cast<QuantLib::MarketModelComposite> (*_ppSingleProductComposite));
    }
}
Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::CSingleProductComposite (PLATFORM::Type^ t) : CMarketModelComposite(CSingleProductComposite::typeid)
{
#ifdef HANDLE
	_phSingleProductComposite = NULL;
#endif
	if (!t->IsSubclassOf(CSingleProductComposite::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::CSingleProductComposite (QuantLib::Handle<QuantLib::SingleProductComposite>& childNative, Object^ owner)  : CMarketModelComposite(CSingleProductComposite::typeid)
{
	_phSingleProductComposite = &childNative;
	_ppSingleProductComposite = &static_cast<boost::shared_ptr<QuantLib::SingleProductComposite>>(childNative.currentLink());
    _ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (boost::dynamic_pointer_cast<QuantLib::MarketModelComposite> (*_ppSingleProductComposite));
    _SingleProductCompositeOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::CSingleProductComposite (QuantLib::Handle<QuantLib::SingleProductComposite> childNative)  : CMarketModelComposite(CSingleProductComposite::typeid)
{
	_phSingleProductComposite = &childNative;
	_ppSingleProductComposite = &static_cast<boost::shared_ptr<QuantLib::SingleProductComposite>>(childNative.currentLink());
    _ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (boost::dynamic_pointer_cast<QuantLib::MarketModelComposite> (*_ppSingleProductComposite));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::CSingleProductComposite (QuantLib::SingleProductComposite childNative)  : CMarketModelComposite(CSingleProductComposite::typeid)
{
#ifdef HANDLE
	_phSingleProductComposite = NULL;
#endif
	_ppSingleProductComposite = new boost::shared_ptr<QuantLib::SingleProductComposite> (new QuantLib::SingleProductComposite (childNative));
    _ppMarketModelComposite = new boost::shared_ptr<QuantLib::MarketModelComposite> (boost::dynamic_pointer_cast<QuantLib::MarketModelComposite> (*_ppSingleProductComposite));
}
#endif

Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::~CSingleProductComposite ()
{
    if (_ppSingleProductComposite != NULL)
    {
	    delete _ppSingleProductComposite;
        _ppSingleProductComposite = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::!CSingleProductComposite ()
{
    if (_ppSingleProductComposite != NULL)
    {
	    delete _ppSingleProductComposite;
    }
}
QuantLib::SingleProductComposite& Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::GetReference ()
{
    if (_ppSingleProductComposite == NULL) throw REFNEW NativeNullException ();
	return **_ppSingleProductComposite;
}
boost::shared_ptr<QuantLib::SingleProductComposite>& Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::GetShared ()
{
    if (_ppSingleProductComposite == NULL) throw REFNEW NativeNullException ();
	return *_ppSingleProductComposite;
}
QuantLib::SingleProductComposite* Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::GetPointer ()
{
    if (_ppSingleProductComposite == NULL) throw REFNEW NativeNullException ();
	return &**_ppSingleProductComposite;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SingleProductComposite>& Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::GetHandle ()
{
	if (_phSingleProductComposite == NULL)
	{
		_phSingleProductComposite = new Handle<QuantLib::SingleProductComposite> (*_ppSingleProductComposite);
	}
	return *_phSingleProductComposite;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::HasNative () 
{
	return (_ppSingleProductComposite != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::Core::IVector<UInt64>^ numberCashFlowsThisStep, Cephei::Core::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ cashFlowsGenerated)
{
    CCurveState^ _CcurrentState;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        numberCashFlowsThisStep->Lock();
        INativeVector<UInt64>^ _NCInumberCashFlowsThisStep = numberCashFlowsThisStep->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCnumberCashFlowsThisStep = safe_cast<CUInt64Vector^>(_NCInumberCashFlowsThisStep);
        std::vector<QuantLib::Size>& _numberCashFlowsThisStep = static_cast<std::vector<QuantLib::Size>&> (_NCnumberCashFlowsThisStep->GetReference ());
        cashFlowsGenerated->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIcashFlowsGenerated = cashFlowsGenerated->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCcashFlowsGenerated = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIcashFlowsGenerated);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _cashFlowsGenerated = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCcashFlowsGenerated->GetReference ());
    	bool _rv = (bool)(*_ppSingleProductComposite)->nextTimeStep ( _currentState,  _numberCashFlowsThisStep,  _cashFlowsGenerated );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (numberCashFlowsThisStep != nullptr) numberCashFlowsThisStep->Unlock(); //NO2  
        if (cashFlowsGenerated != nullptr) cashFlowsGenerated->Unlock(); //NO2  
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSingleProductComposite)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::CSingleProductComposite::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSingleProductComposite)->numberOfProducts ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

